﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace task_1
{
    public static class WebScraper
    {
        private static readonly Regex PhoneRegex = new Regex(@"1[3-9]\d{9}", RegexOptions.Compiled);
        private static readonly Regex EmailRegex = new Regex(@"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", RegexOptions.Compiled);

        public static async Task<(List<string> phones, List<string> emails)> ExtractPhonesAndEmailsAsync(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                string html;

                try
                {
                    html = await client.GetStringAsync(url);
                }
                catch (Exception ex)
                {
                    throw new Exception("网页内容获取失败：" + ex.Message);
                }

                var phones = new HashSet<string>();
                var emails = new HashSet<string>();

                foreach (Match m in PhoneRegex.Matches(html))
                {
                    phones.Add(m.Value);
                }

                foreach (Match m in EmailRegex.Matches(html))
                {
                    emails.Add(m.Value);
                }

                return (new List<string>(phones), new List<string>(emails));
            }
        }
    }
}
